package com.itboye.allshengmedical.sci;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseActivity;
import com.example.allshengmedical.base.BaseApplication;
import com.example.allshengmedical.util.ByAlert;
import com.itboye.allshengmedical.sci.cmd.req.CmdType;
import com.itboye.allshengmedical.sci.packet.req.SetTempReqPacket;
import com.itboye.allshengmedical.sci.packet.resp.BaseRespPacket;
import com.itboye.allshengmedical.sci.packet.resp.GetTempRespPacket;
import com.itboye.allshengmedical.sci.packet.resp.SelfCheckRespPacket;
import com.itboye.allshengmedical.sci.packet.resp.SetTempRespPacket;
import com.itboye.allshengmedical.sci.packet.resp.StopCtrlTempRespPacket;

/**
 * @author boye009
 * 
 */
public class ControlActivity extends BaseActivity {
	// 串口控制器
	private ISerialControl controlInstance;
	// 控件定义
	private ToggleButton btnToggleWRThread;
	private ToggleButton btnToggleComm;
	SetTempReqPacket settemp = new SetTempReqPacket();
	TextView tvsetCtrlMod1;
	TextView tvsetCtrlMod2;
	TextView tvsetCtrlMod3;
	TextView tvLightJust;
	private Button btnVersion;// 版本获取
	private Button btnSelfCheck;// 自检
	private Button btnNoSetTemp;// 不控制目标温度
	private Button setCtrlMod1;// 控制模块1温度
	private Button setCtrlMod2;// 控制模块2温度
	private Button setCtrlMod3;// 控制模块3温度
	private Button btnStopCtrlTemp1;// 停止控制模块1温度
	private Button btnStopCtrlTemp2;// 停止控制模块2温度
	private Button btnStopCtrlTemp3;// 停止控制模块3温度
	private Button btnStayCtrlTemp;// 保持温度
	private Button btnReturnTemp;// 温度返回
	private Button btnEnterVerifyTemp;// 进入校温状态
	private Button btnVerifyTemp1;// 校温模块1
	private Button btnVerifyTemp2;// 校温模块2
	private Button btnVerifyTemp3;// 校温模块3
	private Button btnCheckLight;// 检查光强
	private Button btnLightJust;// 亮度调节
	private Button btnOpenLight1;// 开灯1
	private Button btnOpenLight2;// 开灯2
	private Button btnCloseLight1;// 关灯1
	private Button btnCloseLight2;// 关灯2
	private Button btnUpdate;// 更新

	@Override
	protected int setcontent() {
		return R.layout.activity_control;
	}

	@Override
	protected void initView() {
		controlInstance = DefaultSerialControl.getInstance();
		controlInstance.setHandler(mHandler);
		btnToggleWRThread
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {

						if (controlInstance == null) {
							return;
						}

						if (isChecked) {
							Log.d("serial", "准备开启线程");
							controlInstance.start();
						} else {
							Log.d("serial", "准备暂停线程");
							controlInstance.pause();
						}
					}
				});

		btnToggleComm.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				if (controlInstance == null) {
					return;
				}

				if (isChecked) {
					Log.d("serial", "准备初始化");
					controlInstance.init();
					btnToggleWRThread.setChecked(true);
				} else {
					Log.d("serial", "准备释放资源");
					controlInstance.release();
					btnToggleWRThread.setChecked(false);
				}
			}
		});
		// 按钮点击监听绑定
		btnVersion.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendVersionCmd();
			}
		});

		btnSelfCheck.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendSelfCheckCmd();
			}
		});
		btnNoSetTemp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendSetTempCmd("NoCtrl",
						Integer.parseInt(tvsetCtrlMod1.getText() + ""));
			}

		});
		setCtrlMod1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendSetTempCmd("CtrlTemp1",
						Integer.parseInt(tvsetCtrlMod1.getText() + ""));
			}
		});
		setCtrlMod2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendSetTempCmd("CtrlTemp2",
						Integer.parseInt(tvsetCtrlMod1.getText() + ""));
			}
		});
		setCtrlMod3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendSetTempCmd("CtrlTemp3",
						Integer.parseInt(tvsetCtrlMod1.getText() + ""));
			}
		});
		btnStopCtrlTemp1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendStopTemp("StopTemp1");
			}
		});
		btnStopCtrlTemp2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendStopTemp("StopTemp2");
			}
		});
		btnStopCtrlTemp3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendStopTemp("StopTemp3");
			}
		});
		btnStayCtrlTemp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendStopTemp("StayTemp");
			}
		});
		btnReturnTemp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendReturnTemp();
			}
		});
		btnEnterVerifyTemp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendEnterVerifyTemp();
			}
		});
		btnVerifyTemp1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendVerifyTemp("VerifyTemp1");
			}
		});
		btnVerifyTemp2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendVerifyTemp("VerifyTemp2");
			}
		});
		btnVerifyTemp3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendVerifyTemp("VerifyTemp3");
			}
		});
		btnCheckLight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				controlInstance.addCommand(CmdType.DetectLightIntensitTemp,
						null, 0);
			}
		});
		btnLightJust.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				controlInstance.addCommand(CmdType.LightJustTemp, null,
						Integer.parseInt(tvLightJust.getText() + ""));
			}
		});
		btnOpenLight1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				controlInstance.addCommand(CmdType.OpenLight, null, 0);
			}
		});
		btnOpenLight2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				controlInstance.addCommand(CmdType.OpenLight, "OpenLight2", 0);
			}
		});
		btnCloseLight1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				controlInstance
						.addCommand(CmdType.CloseLight, "CloseLight1", 0);
			}
		});
		btnCloseLight2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				controlInstance
						.addCommand(CmdType.CloseLight, "CloseLight2", 0);
			}
		});
		btnUpdate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				controlInstance.addCommand(CmdType.UpdateArm, null, 0);
				// StringBuilder builder = new StringBuilder();
				// for (int i = 0; i < 97; i += 3) {
				// StringBuffer buffer = new StringBuffer();
				// buffer.append(i);
				// buffer.append(i + 1);
				// buffer.append(i + 2);
				// builder.append(Integer.parseInt(buffer.toString()) + ",");
				// Log.e("hu", buffer.toString());
				// Log.e("hu2", i + "");
				// }
				// Log.e("hu3", builder.toString());
			}
		});

	}

	@Override
	protected void initData() {

	}

	@Override
	protected void onStop() {
		super.onStop();
		if (controlInstance != null) {
			controlInstance.release();
		}
	}

	private static Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			switch (msg.what) {
			case ISerialControl.FindPacketMessage:
				// 完成主界面更新,拿到数据
				update(msg.getData());
				break;
			case ISerialControl.SCTimeoutMessage:
				ByAlert.alert("超时");
				break;
			default:
				break;
			}
		}

	};

	private static void update(Bundle bundle) {

		String cmdType = bundle.getString("cmdType");

		BaseRespPacket packet = (BaseRespPacket) bundle
				.getSerializable("packet");

		if (cmdType.length() < 1)
			return;
		// 根据type 转换packet 为你想要的
		CmdType type = CmdType.getCmdType(cmdType.substring(1));

		// 发送指令后这里会收到数据
		Log.e("read_data", "接受到的响应数据包是" + cmdType + type.toDesc());

		ByAlert.alert("接受到的响应数据包类型" + cmdType + "," + type.toDesc());
		switch (type) {

		case SelfCheck:
			// 转换成自检数据包
			SelfCheckRespPacket selfCheckRespPacket = new SelfCheckRespPacket(
					packet);
			if (selfCheckRespPacket != null) {
				Toast.makeText(BaseApplication.getContext(),
						"【自检错误信息】" + selfCheckRespPacket.getErrorDesc(),
						Toast.LENGTH_LONG).show();
			}
			break;
		case SetTemp:
			SetTempRespPacket setTempRespPacket = new SetTempRespPacket(packet);
			if (setTempRespPacket != null) {
				ByAlert.alert("温度" + setTempRespPacket.getReturnData());
			}

			break;
		case StopCtrlTemp:
			StopCtrlTempRespPacket stopCtrlTempRespPacket = new StopCtrlTempRespPacket(
					packet);
			if (stopCtrlTempRespPacket != null) {
				ByAlert.alert("控温" + stopCtrlTempRespPacket.getReturnData());
			}
			break;
		case GetTemp:
			GetTempRespPacket getTempRespPacket = new GetTempRespPacket(packet);
			if (getTempRespPacket != null) {
				ByAlert.alert("返回温度" + getTempRespPacket.ModeOneTemp());
				Log.e("hu...gettemp", getTempRespPacket.ModeOneTemp());
				Log.e("hu...gettemp", getTempRespPacket.ModeOneTemp());
			}
			break;
		case DetectLightIntensitTemp:
			ByAlert.alert("Light");
			break;
		default:
			break;
		}
	}

	// 发送指令

	private void sendSelfCheckCmd() {
		controlInstance.addCommand(CmdType.SelfCheck, null, 0);
	}

	/**
	 * 
	 */
	private void sendVersionCmd() {
		controlInstance.addCommand(CmdType.Version, null, 0);
	}

	/**
	 * @param todo
	 * @param temp
	 */
	private void sendSetTempCmd(String todo, int temp) {
		controlInstance.addCommand(CmdType.SetTemp, todo, temp);
	}

	private void sendStopTemp(String todo) {
		// TODO Auto-generated method stub
		controlInstance.addCommand(CmdType.StopCtrlTemp, todo, 0);
	}

	private void sendReturnTemp() {
		// TODO Auto-generated method stub
		controlInstance.addCommand(CmdType.GetTemp, null, 0);
	}

	private void sendEnterVerifyTemp() {
		// TODO Auto-generated method stub
		controlInstance.addCommand(CmdType.EnterVerifyTemp, null, 0);
	}

	private void sendVerifyTemp(String todo) {
		// TODO Auto-generated method stub
		controlInstance.addCommand(CmdType.VerifyTemp, todo, 0);
	}
}
